package com.njwb.day11.reflection;

import java.lang.reflect.*;
import java.util.ArrayList;

public class ReflectDemo {
    public static void main(String[] args) throws Exception {
        printClassInfo("java.lang.String");
        printClassInfo("java.lang.StringBuffer");
        printClassInfo("java.com.njwb.util.ArrayList");
    }

    public static void printClassInfo(String className) throws Exception {
        System.out.println("\n==== 类：" + className + " ====");
        Class<?> cls = Class.forName(className);

        // 类基本信息
        System.out.println("完整类名: " + cls.getName());
        System.out.println("简类名: " + cls.getSimpleName());
        System.out.println("包名: " + cls.getPackage());
        System.out.println("是否接口: " + cls.isInterface());
        System.out.println("是否基本类型: " + cls.isPrimitive());
        System.out.println("父类: " + cls.getSuperclass());
        System.out.println("实现的接口: ");
        for (Class<?> iface : cls.getInterfaces()) {
            System.out.println("  " + iface.getName());
        }
        System.out.println("类修饰符: " + Modifier.toString(cls.getModifiers()));

        // 构造方法
        System.out.println("\n【构造方法 - public】");
        for (Constructor<?> c : cls.getConstructors()) {
            System.out.println("  " + c);
        }

        System.out.println("\n【构造方法 - 所有】");
        for (Constructor<?> c : cls.getDeclaredConstructors()) {
            System.out.println("  " + c);
        }

        // 成员变量
        System.out.println("\n【成员变量 - public】");
        for (Field f : cls.getFields()) {
            System.out.println("  " + f);
        }

        System.out.println("\n【成员变量 - 所有】");
        for (Field f : cls.getDeclaredFields()) {
            System.out.println("  " + f);
        }

        // 方法
        System.out.println("\n【方法 - public】");
        for (Method m : cls.getMethods()) {
            System.out.println("  " + m);
        }

        System.out.println("\n【方法 - 所有】");
        for (Method m : cls.getDeclaredMethods()) {
            System.out.println("  " + m);
        }
    }
}
